/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkUtil/MkTime.h"
#include <gtest/gtest.h>

TEST(MkTime, TimeTest)
{
    time_t TmNowTime = time(nullptr);
    CMkTime NowTime = NowMkTime;
    CMkTime TestTime(TmNowTime + 5, 1000);
    CMkTime StrTime("2023-01-07T10:10:10");
    CMkTime UStrTime("2023-01-07T10:10:10.222","T","",TRUE);
    CMkTime TmpTime(2023, 1, 7, 10, 10, 10);
     CMkTime TmpTime2(2023, 1, 7, 10, 10, 10,222000);
     CMkTime TmpTime3(2023, 1, 6, 10, 10, 10,222000);

    MkString strTmpTime;
    Uint32 ErrorCode = TmpTime.GetString(strTmpTime);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_STREQ(strTmpTime.c_str(), "2023-01-07T10:10:10");

    ErrorCode = TmpTime.GetLogString(strTmpTime);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_EQ(strTmpTime.empty(), FALSE);

    ErrorCode = TmpTime.GetFileString(strTmpTime);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_EQ(strTmpTime.empty(), FALSE);

    ErrorCode = TmpTime.GetDayString(strTmpTime);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_EQ(strTmpTime.empty(), FALSE);

    ErrorCode = TmpTime.GetGMTString(strTmpTime);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_EQ(strTmpTime.empty(), FALSE);

    Uint32 MSecond = TestTime.GetMSecond();
    EXPECT_EQ(MSecond, 1);

    Uint64 USecond = TestTime.GetUSecond();
    EXPECT_EQ(USecond, 1000);

    Uint64 TvSecond = TestTime.GetTvSec()-5;
    EXPECT_EQ(TvSecond, TmNowTime);

    Uint64 NtpTime = TestTime.GetNtpTime();
    EXPECT_NE(NtpTime, TmNowTime);

    EXPECT_EQ(StrTime.GetYear(), 2023);
    EXPECT_EQ(StrTime.GetMonth(), 1);
    EXPECT_EQ(StrTime.GetDay(), 7);
    EXPECT_EQ(StrTime.GetHour(), 10);
    EXPECT_EQ(StrTime.GetMinute(), 10);
    EXPECT_EQ(StrTime.GetSecond(), 10);

    BOOL bRet = NowTime > StrTime;
    EXPECT_EQ(bRet, TRUE);

    bRet = NowTime.IsEmpty();
    EXPECT_EQ(bRet, FALSE);

    bRet = UStrTime <= StrTime;
    EXPECT_EQ(bRet, FALSE);

    bRet = UStrTime > StrTime;
    EXPECT_EQ(bRet, TRUE);

    bRet = UStrTime >= StrTime;
    EXPECT_EQ(bRet, TRUE);

    bRet = UStrTime == TmpTime2;
    EXPECT_EQ(bRet, TRUE);

    bRet = UStrTime > TmpTime2;
    EXPECT_EQ(bRet, FALSE);

    bRet = UStrTime < TmpTime2;
    EXPECT_EQ(bRet, FALSE);

    bRet = UStrTime >= TmpTime2;
    EXPECT_EQ(bRet, TRUE);

    bRet = UStrTime <= TmpTime2;
    EXPECT_EQ(bRet, TRUE);

    bRet = TmpTime3 < TmpTime2;
    EXPECT_EQ(bRet, TRUE);
    
    bRet = TmpTime3 <= TmpTime2;
    EXPECT_EQ(bRet, TRUE);

    bRet = TmpTime3 > TmpTime2;
    EXPECT_EQ(bRet, FALSE);

    bRet = TmpTime3 >= TmpTime2;
    EXPECT_EQ(bRet, FALSE);

    bRet = TmpTime3 == TmpTime2;
    EXPECT_EQ(bRet, FALSE);

    bRet = StrTime >= TmpTime;
    EXPECT_EQ(bRet, TRUE);

    bRet = StrTime == TmpTime;
    EXPECT_EQ(bRet, TRUE);

    bRet = StrTime < TmpTime;
    EXPECT_EQ(bRet, FALSE);

    bRet = StrTime <= TmpTime;
    EXPECT_EQ(bRet, TRUE);

    Uint64 offset = TmpTime2 - TmpTime;
    EXPECT_EQ(offset,222000);

    CMkTime Time1 = NowTime - 5 * 1000 * 1000;
    offset = NowTime.OffsetSec(Time1);
    EXPECT_EQ(5, offset);

    Time1 -= 5 * 1000 * 1000;
    offset = NowTime.OffsetSec(Time1);
    EXPECT_EQ(10, offset);

    CMkTime Time2 = Time1 + ((Uint64)60 * 60 * 1000 * 1000);
    offset = Time2.OffsetHour(Time1);
    EXPECT_EQ(1, offset);

    offset = Time1.OffsetHour(Time2);
    EXPECT_EQ(-1, offset);

    Time2 += (5 * 1000 * 1000);

    offset = Time2.OffsetSec(Time1);
    EXPECT_EQ(60 * 60 + 5, offset);
    
    offset = Time1.OffsetSec(Time2);
    EXPECT_EQ(-60 * 60 - 5, offset);
}